(function($_$) {
    $_$.fn.commSlider = function(options) {
        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            prevText: '',
            prevClass: 'shoppingcart_promotions_l02',
            prevgClass: 'shoppingcart_promotions_l',
            nextId: 'nextBtn',
            nextText: '',
            nextClass: 'shoppingcart_promotions_r02',
            nextgClass: 'shoppingcart_promotions_r',
            controlsShow: true,
            controlsBefore: '',
            controlsAfter: '',
            controlsFade: true,
            firstId: 'firstBtn',
            firstText: '',
            firstShow: true,
            lastId: 'lastBtn',
            lastText: '',
            lastShow: true,
            vertical: false,
            speed: 1000,
            auto: false,
            pause: 2000,
            continuous: false
        };
        var options = $_$.extend(defaults, options);

        this.each(function() {
            var obj = $_$(this);
            var s = $_$("li", obj).length;
            var w = $_$("li", obj).width();
            var h = $_$("li", obj).height();
            obj.width(5 * w);
            obj.height(h);
            obj.css("overflow", "hidden");
            var ts = 0;
            if (parseInt((s / 5)) < (s / 5)) {
                ts = parseInt((s / 5));
            }
            else {
                ts = parseInt((s / 5)) - 1;
            }

            var t = 0;
            $_$("ul", obj).css('width', s * w);
            if (!options.vertical) $_$("li", obj).css('float', 'left');

            if (options.controlsShow) {
                var html = ""; //options.controlsBefore;
                if (options.firstShow) html += '<span id="' + options.prevId + '"><a name="nextpage" class="' + options.prevgClass + '"  href=\"javascript:void(0);\">' + options.firstText + '</a></span>';
                $_$(obj).before(html);
                html = '';
                if (options.lastShow) {
                    if (ts == 0) {
                        html += ' <span id="' + options.nextId + '"><a  name="nextpage"  class="' + options.nextgClass + '"  href=\"javascript:void(0);\">' + options.lastText + '</a></span>';
                    } else {
                        html += ' <span id="' + options.nextId + '"><a   name="nextpage"  class="' + options.nextClass + '"  href=\"javascript:void(0);\">' + options.lastText + '</a></span>';
                    }
                }
                html += ""; //options.controlsAfter;
                $_$(obj).after(html);
            };
            $_$("a", "#" + options.nextId).click(function() {
                animate("next", true);
            });
            $_$("a", "#" + options.prevId).click(function() {
                animate("prev", true);
            });
            $_$("a", "#" + options.firstId).click(function() {
                animate("first", true);
            });
            $_$("a", "#" + options.lastId).click(function() {
                animate("last", true);
            });
            function animate(dir, clicked) {
                var ot = t;
                switch (dir) {
                    case "next":
                        t = (ot >= ts) ?  ts : t + 1;
                        break;
                    case "prev":
                        t = (t <= 0) ?   0 : t - 1;
                        break;
                    case "first":
                        t = 0;
                        break;
                    case "last":
                        t = ts;
                        break;
                    default:
                        break;
                };

                var diff = Math.abs(ot - t);
                var speed = diff * options.speed;
                if (!options.vertical) {
                    p = (t * 5 * w * -1);
                    $_$("ul", obj).animate(
                    {
                        marginLeft: p
                    },
                    speed
                    );
                } else {
                    p = (t * h * -1);
                    $_$("ul", obj).animate(
                    {
                        marginTop: p
                    },
                    speed
                    );
                };

                if (options.continuous && options.controlsFade) {

                    if (t == ts) {
                        $_$("a", "#" + options.nextId).attr('class', options.nextgClass);
                    } else {
                        $_$("a", "#" + options.nextId).attr('class', options.nextClass);
                    };
                    if (t == 0) {
                        $_$("a", "#" + options.prevId).attr('class', options.prevgClass);
                    } else {
                        $_$("a", "#" + options.prevId).attr('class', options.prevClass);
                    };
                };

                if (clicked) clearTimeout(timeout);
                if (options.auto && dir == "next" && !clicked) {
                    timeout = setTimeout(function() {
                        animate("next", false);
                    }, diff * options.speed + options.pause);
                };

            };
            // init
            var timeout;
            if (options.auto) {
                ;
                timeout = setTimeout(function() {
                    animate("next", false);
                }, options.pause);
            };

            if (!options.continuous && options.controlsFade) {
                $_$("a", "#" + options.prevId).hide();
                $_$("a", "#" + options.firstId).hide();
            };
        });
    };
})(jQuery);